package cheme.ProcessEditor.Util;

import java.util.List;

import org.eclipse.gef.GraphicalEditPart;


public class SelectionHelper
{
	public static GraphicalEditPart getFirstLeaf(GraphicalEditPart part)
	{
		GraphicalEditPart leaf = part;
		while(0 != leaf.getChildren().size())
		{
			leaf = (GraphicalEditPart)leaf.getChildren().get(0);
		}
		
		return leaf;
	}
	
	public static GraphicalEditPart getSelectionFor(GraphicalEditPart part, int selectionCriteria)
	{
		GraphicalEditPart primarySelection = null;
		
		if(selectionCriteria == part.getSelected() && 0 == part.getChildren().size())
		{
			primarySelection = part;
		}
		else
		{
			primarySelection = getSelectionFromChildren(part.getChildren(), selectionCriteria);
		}
		
		return primarySelection;
	}
	
	public static GraphicalEditPart getSelectionFromChildren(List children, int selectionCriteria)
	{
		GraphicalEditPart primarySelection = null;
		
		for(Object o : children)
		{
			primarySelection = getSelectionFor((GraphicalEditPart)o, selectionCriteria);
			
			if(null != primarySelection)
			{
				break;
			}
		}
		
		return primarySelection;
	}

	public static final int PRECEDING = -1;
	public static final int FOLLOWING = 1;
	
	public static GraphicalEditPart getSibling(int siblingCriteria, GraphicalEditPart part)
	{
		GraphicalEditPart parent = (GraphicalEditPart)part.getParent();
		
		if(null != parent)
		{
			List siblings = parent.getChildren();
			int targetIndex = siblings.indexOf(part) + siblingCriteria;
			
			if(0 <= targetIndex && siblings.size() > targetIndex)
			{
				return (GraphicalEditPart)siblings.get(targetIndex);
			}
		}
		
		return null;
	}
	
////	private void doUp()
////	{
////		field = getSelectionFor(handled, EditPart.SELECTED_PRIMARY);
////		streamComponent = (GraphicalEditPart)field.getParent();
////		List children = streamComponent.getChildren();
////		int index = children.indexOf(field);
////		
////		GraphicalEditPart siblingComponent = getSibling(PRECEDING, streamComponent);
////		
////		if(null != siblingComponent)
////		{
////			List siblingChildren = siblingComponent.getChildren();
////			
////			if(0 != siblingChildren.size() &&
////				(0 <= index && siblingChildren.size() > index))
////			{
////				GraphicalEditPart newField = (GraphicalEditPart)siblingChildren.get(index);
////				
////				newField.setSelected(EditPart.SELECTED_PRIMARY);
////				
////				Command cmd = newField.getCommand(new SelectionRequest());
////				
////				if(null != cmd)
////				{
////					executeCommand(cmd);
////				}
////			}
////		}
////	}
//	
////	/**
////	 * Executes the given command on the command stack.
////	 * @param command the command to execute
////	 */
////	protected void executeCommand(Command command)
////	{
////		try
////		{
////			getEditDomain().getCommandStack().execute(command);
////		}
////		catch(Exception e)
////		{
////			e.printStackTrace();
////		}
////	}
//	
////	public EditDomain getEditDomain()
////	{
////		if(null != handled)
////		{
////			return handled.getViewer().getEditDomain();
////		}
////		
////		return null;
////	}
};
